extends Sprite2D


#炮台
signal DR_open_fire(wz:Vector2,fx :float)

#开火时间
@export var fire_time = 3
#范围
@export var fire_Scope = 300



#是否准备开火
var is_strat = true

func _ready() -> void:
	$Timer.wait_time = fire_time
	$HY.visible = false
	$Suo_di/Suo_di_FW.shape.radius = fire_Scope

#主要负责转向和开火逻辑
func _process(delta: float) -> void:
	Sou_Suo_Di_Ren()
	

#指向最近的敌人
func Sou_Suo_Di_Ren():
	var guards = get_tree().get_nodes_in_group("play")
	for dr in guards:
		var ptWZ = global_position
		var drWZ = dr.position
		var xdWZ = drWZ - ptWZ
		if xdWZ.length() < fire_Scope:
			rotation = xdWZ.angle() - PI/2
			fire(ptWZ,rotation)

#开火
func fire(wx:Vector2,fx:float):
	if is_strat:
		$HY.visible = true
		$HY.play()
		is_strat = false
		$Timer.start()
		DR_open_fire.emit(wx,fx)


#准备开火
func _on_timer_timeout() -> void:
	is_strat = true
	


func _on_hy_animation_finished() -> void:
	$HY.stop()
	$HY.visible = false
